﻿namespace com.vs.Task.BL
{
    using com.vs.DataAccess;
    using com.vs.DataAccess.DO;
    using com.vs.General;
    using com.vs.Inventory.BL;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;

    public class TaskGenerationHelper
    {
        public string CreateRecpPutawayTaskDetail(Session sess, int iTaskNum, DataRow dr)
        {
            ReceiptLocateRequestDO model = ReceiptLocateRequestDO.GetModel(dr);
            return this.CreateTaskDetail(sess, iTaskNum, model);
        }

        public string CreateReplenishTaskDetail(Session sess, int iTaskNum, DataRow dr)
        {
            ReplenishmentRequestDO model = ReplenishmentRequestDO.GetModel(dr);
            return this.CreateTaskDetail(sess, iTaskNum, model);
        }

        public string CreateShipAllocTaskDetail(Session sess, int iTaskNum, DataRow dr)
        {
            ShipmentAllocRequestDO model = ShipmentAllocRequestDO.GetModel(dr);
            return this.CreateTaskDetail(sess, iTaskNum, model);
        }

        public string CreateTaskDetail(Session sess, int iTaskNum, ReceiptLocateRequestDO rlrDO)
        {
            TaskDetailDO ldo = null;
            int num = 0;
            LocationManager manager = null;
            try
            {
                ldo = new TaskDetailDO {
                    INTERNAL_TASK_NUM = iTaskNum,
                    REFERENCE_LINE_NUM = rlrDO.INTERNAL_RECEIPT_LINE_NUM,
                    REFERENCE_CONT_NUM = rlrDO.INTERNAL_CONTAINER_NUM,
                    WAREHOUSE = rlrDO.WAREHOUSE,
                    COMPANY = rlrDO.COMPANY,
                    ITEM = rlrDO.ITEM,
                    FROM_LOC = rlrDO.FROM_LOC,
                    FROM_LPN = rlrDO.CONTAINER_ID,
                    CURRENT_LOC = rlrDO.FROM_LOC,
                    CURRENT_LPN = rlrDO.CONTAINER_ID,
                    TO_LOC = rlrDO.TO_LOC
                };
                manager = new LocationManager();
                if (LocationManager.DoseLocTrackLPN(sess, rlrDO.WAREHOUSE, rlrDO.TO_LOC))
                {
                    ldo.TO_LPN = rlrDO.CONTAINER_ID;
                }
                ldo.ATTRIBUTE_NUM = rlrDO.ATTRIBUTE_NUM;
                ldo.TOTAL_QTY = rlrDO.LOCATE_QTY;
                ldo.set_FROM_QTY(rlrDO.LOCATE_QTY);
                ldo.set_TO_QTY(0);
                ldo.QUANTITY_UM = rlrDO.QUANTITY_UM;
                ldo.CONDITION = TaskConstants.OPEN;
                ldo.DATE_TIME_STAMP = DateTime.Now;
                ldo.INVENTORY_STS = rlrDO.INVENTORY_STS;
                ldo.INTERNAL_TASK_TYPE = TaskConstants.RECEIPT;
                num = ldo.Insert(sess);
                if (num > 0)
                {
                    return (num);
                }
                return MessageManager.MSG_TSK_004;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return (MessageManager.MSG_GEN_000 + exception.Message);
            }
        }

        public string CreateTaskDetail(Session sess, int iTaskNum, ReplenishmentRequestDO rrDO)
        {
            TaskDetailDO ldo = null;
            int num = 0;
            try
            {
                ldo = new TaskDetailDO {
                    INTERNAL_TASK_NUM = iTaskNum,
                    REFERENCE_LINE_NUM = 0,
                    WAREHOUSE = rrDO.WAREHOUSE,
                    COMPANY = rrDO.COMPANY,
                    ITEM = rrDO.ITEM,
                    FROM_LOC = rrDO.FROM_LOC,
                    CURRENT_LOC = rrDO.FROM_LOC,
                    TO_LOC = rrDO.TO_LOC,
                    FROM_LPN = rrDO.FROM_LPN,
                    ATTRIBUTE_NUM = rrDO.ATTRIBUTE_NUM,
                    TOTAL_QTY = rrDO.ALLOCATED_QTY
                };
                ldo.set_FROM_QTY(rrDO.ALLOCATED_QTY);
                ldo.QUANTITY_UM = rrDO.QUANTITY_UM;
                ldo.CONDITION = TaskConstants.OPEN;
                ldo.DATE_TIME_STAMP = DateTime.Now;
                ldo.CURRENT_LOC = rrDO.FROM_LOC;
                ldo.CURRENT_LPN = rrDO.FROM_LPN;
                ldo.INVENTORY_STS = rrDO.INVENTORY_STS;
                ldo.INTERNAL_WAVE_NUM = rrDO.INTERNAL_WAVE_NUM;
                ldo.INTERNAL_TASK_TYPE = TaskConstants.REPLENISHMENT;
                num = ldo.Insert(sess);
                if (num > 0)
                {
                    return (num);
                }
                return MessageManager.MSG_TSK_004;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return (MessageManager.MSG_GEN_000 + exception.Message);
            }
        }

        public string CreateTaskDetail(Session sess, int iTaskNum, ShipmentAllocRequestDO sarDO)
        {
            TaskDetailDO ldo = null;
            int num = 0;
            try
            {
                ldo = new TaskDetailDO {
                    INTERNAL_TASK_NUM = iTaskNum,
                    REFERENCE_LINE_NUM = sarDO.INTERNAL_SHIPMENT_LINE_NUM,
                    WAREHOUSE = sarDO.WAREHOUSE,
                    COMPANY = sarDO.COMPANY,
                    ITEM = sarDO.ITEM,
                    FROM_LOC = sarDO.FROM_LOC,
                    CURRENT_LOC = sarDO.FROM_LOC,
                    TO_LOC = sarDO.TO_LOC,
                    FROM_LPN = sarDO.FROM_LPN,
                    ATTRIBUTE_NUM = sarDO.ATTRIBUTE_NUM,
                    TOTAL_QTY = sarDO.ALLOCATED_QTY
                };
                ldo.set_FROM_QTY(sarDO.ALLOCATED_QTY);
                ldo.QUANTITY_UM = sarDO.QUANTITY_UM;
                ldo.CONDITION = TaskConstants.OPEN;
                ldo.DATE_TIME_STAMP = DateTime.Now;
                ldo.CURRENT_LOC = sarDO.FROM_LOC;
                ldo.CURRENT_LPN = sarDO.FROM_LPN;
                ldo.INVENTORY_STS = sarDO.INVENTORY_STS;
                ldo.INTERNAL_WAVE_NUM = sarDO.INTERNAL_WAVE_NUM;
                ldo.REFERENCE_REQ_NUM = sarDO.INTERNAL_SHIP_ALLOC_NUM;
                ldo.INTERNAL_TASK_TYPE = TaskConstants.SHIPMENT;
                num = ldo.Insert(sess);
                if (num > 0)
                {
                    return (num);
                }
                return MessageManager.MSG_TSK_004;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return (MessageManager.MSG_GEN_000 + exception.Message);
            }
        }

        public string CreateTaskDetail(Session sess, int iTaskNum, int iRefLineNum, InventoryTransDS invTranDS)
        {
            TaskDetailDO ldo = null;
            int num = 0;
            try
            {
                ldo = new TaskDetailDO {
                    INTERNAL_TASK_NUM = iTaskNum,
                    REFERENCE_LINE_NUM = iRefLineNum,
                    WAREHOUSE = invTranDS.WAREHOUSE,
                    COMPANY = invTranDS.COMPANY,
                    ITEM = invTranDS.ITEM,
                    FROM_LOC = invTranDS.FROM_LOC,
                    CURRENT_LOC = invTranDS.FROM_LOC,
                    TO_LOC = invTranDS.TO_LOC,
                    TO_LPN = invTranDS.TO_LPN,
                    FROM_LPN = invTranDS.FROM_LPN,
                    TO_LPN = invTranDS.TO_LPN,
                    ATTRIBUTE_NUM = invTranDS.ATTRIBUTE_NUM,
                    TOTAL_QTY = invTranDS.QUANTITY
                };
                ldo.set_FROM_QTY(invTranDS.QUANTITY);
                ldo.set_TO_QTY(0);
                ldo.QUANTITY_UM = invTranDS.QUANTITY_UM;
                ldo.CONDITION = TaskConstants.OPEN;
                ldo.DATE_TIME_STAMP = DateTime.Now;
                ldo.CURRENT_LOC = invTranDS.FROM_LOC;
                ldo.CURRENT_LPN = invTranDS.FROM_LPN;
                ldo.INVENTORY_STS = invTranDS.INVENTORY_STS;
                num = ldo.Insert(sess);
                if (num > 0)
                {
                    return (num);
                }
                return MessageManager.MSG_TSK_004;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return MessageManager.MSG_GEN_000;
            }
        }

        public string CreateTaskDetail(Session sess, string stRecordType, int iTaskNum, DataRow dr)
        {
            string msg = null;
            if (StringManager.Equals(stRecordType, "RECV_PUTAWAY_TASK"))
            {
                msg = this.CreateRecpPutawayTaskDetail(sess, iTaskNum, dr);
            }
            else if (StringManager.Equals(stRecordType, "SHIP_ALLOC_TASK"))
            {
                msg = this.CreateShipAllocTaskDetail(sess, iTaskNum, dr);
            }
            else if (StringManager.Equals(stRecordType, "REPLENISH_TASK"))
            {
                msg = this.CreateReplenishTaskDetail(sess, iTaskNum, dr);
            }
            if (StringManager.IsErrorMessage(msg) && !StringManager.IsEmpty(this.deleteIncompleteTask(sess, iTaskNum)))
            {
            }
            return msg;
        }

        public string CreateTaskHeader(Session sess, string stTaskIDField, string stTaskType, int iRefNum, string stRefType, string stRefID, string stIntTaskType, string stWarehouse, string stCompany, string stHoldCode, int iIntWaveNum)
        {
            int num = 0;
            try
            {
                num = new TaskHeaderDO { TASK_TYPE = stTaskType, REFERENCE_NUM = iRefNum, REFERENCE_NUM_TYPE = stRefType, REFERENCE_ID = stRefID, INTERNAL_TASK_TYPE = stIntTaskType, WAREHOUSE = stWarehouse, COMPANY = stCompany, USER_STAMP = sess.User, CONDITION = TaskConstants.OPEN, HOLD_CODE = stHoldCode, INTERNAL_WAVE_NUM = iIntWaveNum }.Insert(sess);
                if (num > 0)
                {
                    return (num);
                }
                return MessageManager.MSG_TSK_004;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return (MessageManager.MSG_GEN_000 + exception.Message);
            }
        }

        private string CreateTasksByMaster(Session sess, int iWaveNum, TaskCreationMasterDO tcmDO)
        {
            string cmdText = null;
            DataTable table = null;
            DataTable table2 = null;
            StringBuilder builder = null;
            StringBuilder builder2 = null;
            string str2 = null;
            string str3 = null;
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam = null;
            List<string> list2 = null;
            string item = null;
            string stRefType = null;
            string str = null;
            string referenceNumField = null;
            string referenceLineNumField = null;
            string stTaskID = null;
            string msg = null;
            bool flag = false;
            int iTaskNum = 0;
            int iRefNum = 0;
            int @int = 0;
            StringBuilder builder3 = null;
            try
            {
                if (tcmDO == null)
                {
                    return MessageManager.MSG_WAV_012;
                }
                builder3 = new StringBuilder();
                str2 = tcmDO.FILTER_NAME;
                str3 = tcmDO.RECORD_TYPE;
                dcParam = new List<KeyValuePair<string, KeyValuePair<string, object>>>();
                KeyValuePair<string, KeyValuePair<string, object>> pair = new KeyValuePair<string, KeyValuePair<string, object>>("RECORD_TYPE", new KeyValuePair<string, object>("=", str3));
                dcParam.Add(pair);
                pair = new KeyValuePair<string, KeyValuePair<string, object>>("FILTER_NAME", new KeyValuePair<string, object>("=", str2));
                dcParam.Add(pair);
                cmdText = DataManager.GetString(FilterConfigDetailDO.GetTableByColumns(sess, dcParam, null).Rows[0], "FILTER_STATEMENT");
                table2 = new DataHelper(sess).GetTable(CommandType.Text, cmdText, new IDataParameter[] { DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum) });
                if (!DataManager.IsEmpty(table2))
                {
                    pair = new KeyValuePair<string, KeyValuePair<string, object>>("CREATE_BREAK", new KeyValuePair<string, object>("=", "Y"));
                    dcParam.Add(pair);
                    table = FilterOrderByDO.GetTableByColumns(sess, dcParam, " ORDER BY SEQUENCE ASC");
                    list2 = new List<string>();
                    foreach (DataRow row in table.Rows)
                    {
                        item = DataManager.GetString(row, "ATTRIBUTE");
                        item = item.Substring(item.IndexOf('.') + 1);
                        list2.Add(item);
                    }
                    builder2 = new StringBuilder();
                    builder = new StringBuilder();
                    bool flag2 = true;
                    stRefType = this.GetReferenceTypeField(sess, tcmDO);
                    str = this.GetReferenceIDField(sess, tcmDO);
                    referenceNumField = this.GetReferenceNumField(sess, tcmDO);
                    referenceLineNumField = this.GetReferenceLineNumField(sess, tcmDO);
                    foreach (DataRow row in table2.Rows)
                    {
                        string str11;
                        builder2 = new StringBuilder();
                        foreach (string str14 in list2)
                        {
                            builder2.Append(DataManager.GetString(row, str14)).Append("|");
                        }
                        if (!StringManager.IsEmpty(str))
                        {
                            str11 = DataManager.GetString(row, str);
                        }
                        else
                        {
                            str11 = null;
                        }
                        if (!StringManager.IsEmpty(referenceNumField))
                        {
                            iRefNum = DataManager.GetInt(row, referenceNumField);
                        }
                        else
                        {
                            iRefNum = 0;
                        }
                        if (!StringManager.IsEmpty(referenceLineNumField))
                        {
                            @int = DataManager.GetInt(row, referenceLineNumField);
                        }
                        else
                        {
                            @int = 0;
                        }
                        if (flag2)
                        {
                            flag2 = false;
                            builder = new StringBuilder().Append(builder2);
                            msg = this.CreateTaskHeader(sess, tcmDO.TASK_ID_FIELD, tcmDO.TASK_TYPE, iRefNum, stRefType, str11, tcmDO.INTERNAL_TASK_TYPE, DataManager.GetString(row, "WAREHOUSE"), DataManager.GetString(row, "COMPANY"), null, iWaveNum);
                            if (StringManager.IsErrorMessage(msg))
                            {
                                builder3.AppendLine(msg);
                                flag = true;
                            }
                            else
                            {
                                iTaskNum = Convert.ToInt32(msg);
                            }
                        }
                        if (StringManager.Equals(builder2.ToString(), builder.ToString()))
                        {
                            if (flag)
                            {
                                continue;
                            }
                            msg = this.CreateTaskDetail(sess, tcmDO.RECORD_TYPE, iTaskNum, row);
                            if (StringManager.IsErrorMessage(msg))
                            {
                                builder3.AppendLine(msg);
                                flag = true;
                            }
                        }
                        else
                        {
                            builder = new StringBuilder().Append(builder2);
                            stTaskID = this.GetDesiredTaskID(sess, iTaskNum, tcmDO.TASK_ID_FIELD);
                            this.SetUniqueTaskID(sess, iTaskNum, stTaskID);
                            msg = this.CreateTaskHeader(sess, tcmDO.TASK_ID_FIELD, tcmDO.TASK_TYPE, iRefNum, stRefType, str11, tcmDO.INTERNAL_TASK_TYPE, DataManager.GetString(row, "WAREHOUSE"), DataManager.GetString(row, "COMPANY"), null, iWaveNum);
                            if (StringManager.IsErrorMessage(msg))
                            {
                                builder3.AppendLine(msg);
                                flag = true;
                            }
                            else
                            {
                                iTaskNum = Convert.ToInt32(msg);
                            }
                            msg = this.CreateTaskDetail(sess, tcmDO.RECORD_TYPE, iTaskNum, row);
                            if (StringManager.IsErrorMessage(msg))
                            {
                                builder3.AppendLine(msg);
                                flag = true;
                            }
                        }
                    }
                    stTaskID = this.GetDesiredTaskID(sess, iTaskNum, tcmDO.TASK_ID_FIELD);
                    this.SetUniqueTaskID(sess, iTaskNum, stTaskID);
                    if (!StringManager.IsEmpty(builder3))
                    {
                        return builder3.ToString();
                    }
                }
                return null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return MessageManager.MSG_GEN_000;
            }
        }

        public string CreateWaveTasksByMaster(Session sess, int iWaveNum, string stRecordType)
        {
            List<TaskCreationMasterDO> list = null;
            string str = null;
            StringBuilder builder = null;
            try
            {
                list = new List<TaskCreationMasterDO>();
                list = TaskCreationMasterDO.GetListByColumn(sess, "RECORD_TYPE", stRecordType);
                if (list.Count < 1)
                {
                    HistoryManager.SaveProcessHistory(sess, HistoryManager.ProcessType.iTask, HistoryManager.Condition.iFailed, MessageManager.MSG_WAV_012);
                    return null;
                }
                builder = new StringBuilder();
                foreach (TaskCreationMasterDO rdo in list)
                {
                    str = this.CreateTasksByMaster(sess, iWaveNum, rdo);
                    if (!StringManager.IsEmpty(str))
                    {
                        builder.AppendLine(str);
                    }
                }
                if (!StringManager.IsEmpty(builder))
                {
                    return builder.ToString();
                }
                return null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return (MessageManager.MSG_GEN_000 + exception.Message);
            }
        }

        private string deleteIncompleteTask(Session sess, int iTaskNum)
        {
            DataHelper helper = null;
            string str;
            try
            {
                helper = new DataHelper(sess);
                str = helper.GetString(CommandType.StoredProcedure, "TSK_DIncompleteTaskInWave", new IDataParameter[] { DataHelper.BuildParameter(sess, "INTERNAL_TASK_NUM", iTaskNum) });
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                str = MessageManager.MSG_GEN_000 + exception.Message;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return str;
        }

        public string DeleteTask(Session sess, int iTaskNum)
        {
            return null;
        }

        public string DeleteTaskDetail(Session sess, int iTaskLnNum)
        {
            return null;
        }

        private string GetDesiredTaskID(Session sess, int iTaskNum, string stTaskIDField)
        {
            DataHelper helper = null;
            string str = null;
            string str2;
            try
            {
                helper = new DataHelper(sess);
                str = helper.GetString(CommandType.StoredProcedure, "TSK_RDesiredTaskID", new IDataParameter[] { DataHelper.BuildParameter(sess, "@INTERNAL_TASK_NUM", iTaskNum), DataHelper.BuildParameter(sess, "@TASK_ID_FIELD", stTaskIDField) });
                if (StringManager.IsEmpty(str))
                {
                    str = iTaskNum;
                }
                str2 = str;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                str2 = iTaskNum;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return str2;
        }

        private string GetReferenceIDField(Session sess, TaskCreationMasterDO tcmDO)
        {
            if ((tcmDO.RECORD_TYPE == "SHIP_ALLOC_TASK") || (tcmDO.RECORD_TYPE == "SHIP_CONT_TASK"))
            {
                return "SHIPMENT_ID";
            }
            if (tcmDO.RECORD_TYPE == "RECV_PUTAWAY_TASK")
            {
                return "RECEIPT_ID";
            }
            return null;
        }

        private string GetReferenceLineNumField(Session sess, TaskCreationMasterDO tcmDO)
        {
            if ((tcmDO.RECORD_TYPE == TaskConstants.SHIP_ALLOC_TASK) || (tcmDO.RECORD_TYPE == TaskConstants.SHIP_CONT_TASK))
            {
                return "INTERNAL_SHIPMENT_NUM";
            }
            if (tcmDO.RECORD_TYPE == TaskConstants.RECV_PUTAWAY_TASK)
            {
                return "INTERNAL_RECEIPT_NUM";
            }
            return null;
        }

        private string GetReferenceNumField(Session sess, TaskCreationMasterDO tcmDO)
        {
            if ((tcmDO.RECORD_TYPE == "SHIP_ALLOC_TASK") || (tcmDO.RECORD_TYPE == "SHIP_CONT_TASK"))
            {
                return "INTERNAL_SHIPMENT_NUM";
            }
            if (tcmDO.RECORD_TYPE == "RECV_PUTAWAY_TASK")
            {
                return "INTERNAL_RECEIPT_NUM";
            }
            return null;
        }

        private string GetReferenceTypeField(Session sess, TaskCreationMasterDO tcmDO)
        {
            if ((tcmDO.RECORD_TYPE == "SHIP_ALLOC_TASK") || (tcmDO.RECORD_TYPE == "SHIP_CONT_TASK"))
            {
                return "SHIPMENT";
            }
            if (tcmDO.RECORD_TYPE == "REPLENISH_TASK")
            {
                return "REPLENISHMENT";
            }
            if (tcmDO.RECORD_TYPE == "RECV_PUTAWAY_TASK")
            {
                return "RECEIPT";
            }
            return null;
        }

        private string SetUniqueTaskID(Session sess, int iTaskNum, string stTaskID)
        {
            DataHelper helper = null;
            string str;
            try
            {
                helper = new DataHelper(sess);
                helper.GetString(CommandType.StoredProcedure, "TSK_UUniqueTaskID", new IDataParameter[] { DataHelper.BuildParameter(sess, "@INTERNAL_TASK_NUM", iTaskNum), DataHelper.BuildParameter(sess, "@TASK_ID", stTaskID) });
                str = null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                str = MessageManager.MSG_GEN_000 + exception.Message;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return str;
        }
    }
}

